package _06_动态规划;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class _139_单词拆分 {

    // 根据前面的推出后面的
    public boolean wordBreak(String s, List<String> wordDict) {
        HashSet<String> hashSet = new HashSet<>(wordDict);
        int len = s.length();
        // 定义前i个字符串是否能在字典中找到
        boolean[] dp = new boolean[len + 1];
        dp[0] = true;
        for (int i = 1; i <= len; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && hashSet.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[len];
    }

}
